<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\material;

use app\model\FileUploadModel;
use app\service\BaseService;
use think\Model;

/**
 * 系统素材基础服务类.
 */
abstract class MaterialService extends BaseService
{
    /**
     * 同步系统素材.
     *
     * @param Model $model 模型
     */
    public function startSync(Model $model): bool
    {
        if (! $this->setData($model)) {
            return false;
        }
        if (! $this->finishSync($model)) {
            return false;
        }

        return true;
    }

    /**
     * 设置素材同步完成.
     *
     * @param Model $model 模型
     */
    public function finishSync(Model $model): bool
    {
        if (! $model->getKey()) {
            return self::setError('未知素材模型');
        }
        $model->setAttr('is_mark', 1);
        return $model->save();
    }

    /**
     * 获取文件模型.
     * @param int $id 文件id
     * @return array|FileUploadModel|mixed|Model
     */
    public function getFile(int $id = 0)
    {
        return (new FileUploadModel())->where('id', $id)->findOrEmpty();
    }
}
